<?php
//此处定义加密信息
const AES_KEY = '12f862d21dcfeafb57bckfrrt5yuiopf';
const IV = '12f862d21dcfeafb';

/**
 * 发送API错误
 *
 * @param string $code 编码
 * @param string $error 错误内容
 */
function send_api_error($code, $error)
{
    $arr = array("result_code" => $code, "result_info" => $error);

    if (!input('get.no_encrypt')) {
        $body = aes_encrypt(json_encode($arr, JSON_UNESCAPED_UNICODE));
        return json(['body' => $body]);
    } else {
        return json($arr);
    }
}

/*****************************错误编码****************************/
//登录异常错误
const H_API_ERROR_TOKEN = "1111";
//用户输入错误
const H_API_ERROR_USER = "2222";
//APP开发者错误
const H_API_ERROR_DEVELOPER = "3333";
//服务器端开发者错误
const H_API_ERROR_SERVER = "4444";
//用户未登录
const H_API_ERROR_LOGIN = "5555";


/**
 * 发送服务器级别信息
 *
 * @param string $error 错误信息
 */
function send_server_error($error = "")
{
    return send_api_error(H_API_ERROR_SERVER, $error);
}

/**
 * 发送开发者级别错误信息
 *
 * @param string $error 错误信息
 */
function send_developer_error($error = "")
{
    return send_api_error(H_API_ERROR_DEVELOPER, $error);
}

/**
 * 发送用户输入错误
 *
 * @param string $error
 */
function send_user_error($error = "")
{
    return send_api_error(H_API_ERROR_USER, $error);
}

/**
 * 发送未登录错误
 *
 * @param string $error
 */
function send_login_error($error = "")
{
    return send_api_error(H_API_ERROR_LOGIN, $error);
}

/**
 * 发送登录异常
 *
 * @param string $error
 */
function send_token_error($error = "")
{
    return send_api_error(H_API_ERROR_TOKEN, $error);
}


/**
 * 生成随机ID
 *
 * @return string                       生成的ID
 */
function generate_random_id($bit = 8)
{
    //抽奖码
    $baseChar = '123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    $id = '';
    for($i = 0; $i < $bit; $i++)
    {
        $rand = rand(0, strlen($baseChar) - 1);
        $id .= $baseChar[$rand];
    }

    return $id;
}

/**
 * 翻译字符串
 *
 * @param string $message 待翻译的文本
 * @return mixed
 */
function translate($message)
{
    return mo_translate($message, "api");
}

/**
 * 删除注入
 * @param $keyword
 */
function removeInjection($keyword){
    $keyword = str_replace(' ','',$keyword);
    $keyword = str_replace('\r\n','',$keyword);
    $keyword = str_replace('\n','',$keyword);
    $keyword = str_replace('\r','',$keyword);
    $keyword = str_replace('<br>','',$keyword);
    $keyword = htmlspecialchars($keyword);
    return $keyword;
}

/**
 * @return bool
 */
function is_https() {
    if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return true;
    } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
        return true;
    } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        return true;
    }
    return false;
}


/**
 * 加密方法
 * @param string $str
 * @return string
 */
function aes_encrypt($ciphertext, $key = '')
{
    if($key){
        $aes_key = $key;
    }else{
        $aes_key = AES_KEY;
    }
    return base64_encode(openssl_encrypt($ciphertext, 'AES-256-CBC',$aes_key, OPENSSL_RAW_DATA , IV));
}

/**
 * 解密方法
 * @param string $str
 * @return string
 */
function aes_decrypt($ciphertext, $key = '')
{
    if($key){
        $aes_key = $key;
    }else{
        $aes_key = AES_KEY;
    }
    return openssl_decrypt(base64_decode($ciphertext), 'AES-256-CBC', $aes_key, OPENSSL_RAW_DATA, IV);
}



